var scripts = document.getElementsByTagName('script');
// get all script tags so we can find this one.
var id = null;
var uo_custom_styles = "";
// placeholder for the fid
for(var i = 0; i < scripts.length; i++) 
{
	// loop through all script tags
	var src = scripts[i].src.split('/').pop();
	// get the src
	match = src.match(/online\.js\?id\=([a-zA-Z0-9]+)/);
	// match for this file
	if ( match ) 
	{
		// this is the right script
		id = match[1];
		// retrieve the fid    
    uo_custom_styles = scripts[i].getAttribute("values");
	}
}
if ( id ) 
{
  var uo_style = uo_getParameterByName('style');

  if(uo_style.length>0 && uo_style=='text'){
    var uo_head_b = document.getElementsByTagName('head')[0];
    var uo_script_b = document.createElement('script');
    uo_script_b.setAttribute('type', 'text/javascript');
    uo_script_b.setAttribute('src', 'http://totallylayouts.com/tumblr/users-online-counter/counter_text.php?id='+id);
    uo_script_b.setAttribute('id', 'script_uo_code_b');
    var uo_script_b_id = document.getElementById('script_uo_code_b');
    if(uo_script_b_id){
        uo_head_b.removeChild(uo_script_b_id);
    }
    // Insert <script> into DOM
    uo_head_b.appendChild(uo_script_b);

    if(uo_custom_styles.length>0)
    document.write( '<a href="'+destination+'" id="uo_counter_text" style="'+uo_custom_styles+'"></a>');
    else
    document.write( '<a href="'+destination+'" id="uo_counter_text"></a>');
  }else{
    // if found the fid
  	document.write( '<a href="'+destination+'"><img src="'+image_url+'?id='+id+'" alt="'+ref_text+'" border="0"></a>');
  	// display the image, etc
  }

	
}

function uo_getParameterByName(tbg_name)
{
  var tbg_script_location = uo_getScriptQuery('online.js');  
  tbg_name = tbg_name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");  
  var tbg_regexS = "[\\?&]" + tbg_name + "=([^&#]*)";
  var tbg_regex = new RegExp(tbg_regexS);
  var tbg_results = tbg_regex.exec(tbg_script_location);  
  if(tbg_results == null)
    return "";
  else
    return decodeURIComponent(tbg_results[1].replace(/\+/g, " "));
}

function show_uo_counter(c) {
 var uo_counter_text = document.getElementById('uo_counter_text');
 uo_counter_text.innerHTML = c; 
}

function uo_getScriptQuery(uo_identifier) {
 var uo_script_bs = document.getElementsByTagName('script'),
     uo_i, uo_curScript;

 for (uo_i = 0; uo_i < uo_script_bs.length; ++uo_i) {
  uo_curScript = uo_script_bs[uo_i];
  
  if (uo_curScript.src.match(uo_identifier)) {  
   return (uo_curScript.src.match(/\?.*/) || [undefined])[0];
  }
 }
}
